package com.atguigui.leetcode1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1233.删除子文件夹
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/2/8 11:07
 */
public class P1233RemoveSubFoldersFromTheFilesystem {

    public static void main(String[] args) {
        Solution solution = new P1233RemoveSubFoldersFromTheFilesystem().new Solution();
        // TO TEST
    }

    class Solution {
        public List<String> removeSubfolders(String[] folder) {
            Arrays.sort(folder);
            List<String> ans = new ArrayList<String>();
            ans.add(folder[0]);
            for (int i = 1; i < folder.length; ++i) {
                //得到前一个字符串长度
                int pre = ans.get(ans.size() - 1).length();
                //截取字符串，如果截取的字符串和前一个相等且最后一个不为'/'，则删除该目录
                if (!(pre < folder[i].length() && ans.get(ans.size() - 1).equals(folder[i].substring(0, pre)) && folder[i].charAt(pre) == '/')) {
                    ans.add(folder[i]);
                }
            }
            return ans;
        }
    }
}
